public without sharing class InstrumentDeliverynDetailsHandler {
 	
 	//重新计算 仪器发货数量
	public static void calcSendEquipmentNum(List<InstrumentDeliverynDetails__c> list_new  ){

		Set<Id> mSets = new Set<Id>();

		for (InstrumentDeliverynDetails__c idd  : list_new) {

			mSets.add(idd.ReturnToMainTable__c);

		}
	

		for (InstrumentDeliveryApplication__c ida  :
			[select Id ,DeliveryRelatedContracts__c from InstrumentDeliveryApplication__c where Id in:mSets and DeliveryRelatedContracts__c != null   ] ) {
			
			ContractCalcEquipmentUtil.sendBackToContract(ida.DeliveryRelatedContracts__c);

		}


	}


	//同步NC的时候 自动生成安装申请 按仪器生成
    public static void genInstallApply( List<InstrumentDeliverynDetails__c> list_new ,Map<Id,InstrumentDeliverynDetails__c> oldMap ){

    	Set<Id> detailSet = new Set<Id>();
    	for(InstrumentDeliverynDetails__c idd : list_new){
    		InstrumentDeliverynDetails__c old = oldMap.get(idd.Id);
    		if(idd.IsSycToNc__c == true && old.IsSycToNc__c != idd.IsSycToNc__c){
    			detailSet.add(idd.Id);
    		}

    	}
    	if(detailSet.isEmpty())return;

    	Map<String,String> outMap = ImportObject.getPicklistValues('InstrumentDeliveryApplication__c', 'InventoryStatus__c', false);
    	List<InstrumentDeliverynDetails__c> iddList = [select Id,DeliveryInstrument__c,Number__c,DeliveryInstrument__r.Product__c,
    		DeliveryInstrument__r.Contract__r.Account__c,DeliveryInstrument__r.Contract__r.Account__r.RecordType.DeveloperName,
    		DeliveryInstrument__r.Contract__r.Maccura__c,DeliveryInstrument__r.Contract__c,DeliveryInstrument__r.EnterWay__c,
    		ReturnToMainTable__r.InventoryStatus__c,
    		DeliveryInstrument__r.Opportunity__c,
    		FuselageNumber__c,LineType__c,
    		ReturnToMainTable__r.ReceivingCustomer__c,ReturnToMainTable__r.OwnerId,
    		ReturnToMainTable__r.ReceivingCustomer__r.System__c,
    		ReturnToMainTable__r.ReceivingCustomer__r.RecordType.DeveloperName , 
    		ReturnToMainTable__r.ReceivingCustomer__r.Category__c,
    		ReturnToMainTable__r.InvoicingCustomers__r.RecordType.DeveloperName,
    		ReturnToMainTable__r.InvoicingCustomers__c,
    		ReturnToMainTable__r.ReceiverTelephone__c ,ReturnToMainTable__r.ReceivingAddress__c ,
			ReturnToMainTable__r.TimeReceipt__c ,ReturnToMainTable__r.RequestReceive__c ,
			ReturnToMainTable__r.Reagents__c ,ReturnToMainTable__r.ReceivingDepartment__c ,
			ReturnToMainTable__r.RequestArrivalDate__c ,ReturnToMainTable__r.SmallSteps__c ,
			ReturnToMainTable__r.MaximumCornerWidth__c ,ReturnToMainTable__r.StepNo__c ,
			ReturnToMainTable__r.Remark__c ,ReturnToMainTable__r.LaboratoryFloor__c ,
			ReturnToMainTable__r.Elevator__c ,ReturnToMainTable__r.StaircaseWidth__c ,
			ReturnToMainTable__r.SiteAssistConsignee__c ,ReturnToMainTable__r.ElevatorSize__c ,
			ReturnToMainTable__r.ElevatorLoad__c ,ReturnToMainTable__r.DoorwaySize__c ,
			ReturnToMainTable__r.IntoCityTime__c ,ReturnToMainTable__r.UnloadingPoint__c ,
			ReturnToMainTable__r.TransportChannel__c ,ReturnToMainTable__r.SceneTakeDelivery__c ,
			ReturnToMainTable__r.LimitPeriod__c 

    		from InstrumentDeliverynDetails__c where Id in:detailSet and DeliveryInstrument__r.Product__r.ContractDetails__c = '仪器类(生成安装申请)'  ];
    	//<合同仪器Id,安装申请的数量> 已生成安装申请的数量
    	Map<String ,Integer> cpMap = new Map<String,Integer>();
    	Map<String,String> dealerMap = new Map<String,String >();
    	for (InstrumentDeliverynDetails__c idd  : iddList) {
    		cpMap.put(idd.Id, 0);
    		dealerMap.put(idd.DeliveryInstrument__r.Contract__r.Account__r.RecordType.DeveloperName == 'Dealer' ? idd.DeliveryInstrument__r.Contract__r.Account__c : idd.DeliveryInstrument__r.Contract__r.Maccura__c , null);
    		dealerMap.put(idd.ReturnToMainTable__r.InvoicingCustomers__c ,null);

    	}
    		System.debug('genInstallApply==>'+cpMap);

    	//查找经销商
    		Map<String,Id> supMap = new Map<String,Id>();
    	for (Dealer__c d  :[select Id , RelatedCustomers__c,RelatedCustomers__r.Name  from Dealer__c where RelatedCustomers__c in: dealerMap.keySet()  or RelatedCustomers__r.Name in: outMap.values()   ] ) {
    		dealerMap.put(d.RelatedCustomers__c,d.Id);
    		supMap.put(d.RelatedCustomers__r.Name , d.Id );
    	}

    		//System.debug('genInstallApply==>'+supMap);
    		//System.debug('genInstallApply==>'+outMap);

       //查询是否已经建立的的安装申请
       for(AggregateResult arg :[select count(Id) total ,InstallInstruments__c from InstallationApply__c where InstallInstruments__c in: cpMap.keySet() group by InstallInstruments__c]){

       		String cpId = String.valueOf(arg.get('InstallInstruments__c'));
       		cpMap.put(cpId , Integer.valueOf(arg.get('total')));

       }
	

    	List<InstallationApply__c> installList = new List<InstallationApply__c>();
    	
    	for (InstrumentDeliverynDetails__c idd  : iddList) {

    		//已安装数量
    		Integer cnt = Integer.valueOf(idd.Number__c);
    		cnt -= cpMap.get(idd.Id );
    		 List<String> enos = idd.FuselageNumber__c == null ? new List<String>() : idd.FuselageNumber__c.contains('*') ? idd.FuselageNumber__c.split('\\*',-1) : new List<String>{idd.FuselageNumber__c };
    		for (Integer i = 0 ; i < cnt ; i ++ ) {

    			InstallationApply__c insa = new InstallationApply__c();
	    		//	合同仪器
	    		insa.ContractEquipment__c = idd.DeliveryInstrument__c;
	    		insa.Contract__c = idd.DeliveryInstrument__r.Contract__c;
	    		//医院
	    		insa.Hospital__c = (idd.ReturnToMainTable__r.ReceivingCustomer__r.System__c != null || 
	    		idd.ReturnToMainTable__r.ReceivingCustomer__r.RecordType.DeveloperName == 'TerminalCustomer' || 
	    		idd.ReturnToMainTable__r.ReceivingCustomer__r.Category__c  == '实验室	' || 
	    		idd.ReturnToMainTable__r.ReceivingCustomer__r.Category__c  == '体检中心' ) ?   idd.ReturnToMainTable__r.ReceivingCustomer__c : null ;// ida.ReceivingCustomer__c;

	    		insa.Opp__c = idd.DeliveryInstrument__r.Opportunity__c;
	    		//流水线类型
	    		insa.LineType__c = idd.LineType__c;
	    		//
	    		insa.Instrument__c = idd.DeliveryInstrument__r.Product__c;
	    		//供应商
	    		//insa.Supplier__c = idd.DeliveryInstrument__r.Contract__r.Account__r.RecordType.DeveloperName == 'Dealer' ? idd.DeliveryInstrument__r.Contract__r.Account__c : idd.DeliveryInstrument__r.Contract__r.Maccura__c ;
	    		if(idd.DeliveryInstrument__r.Contract__c != null )insa.NEWDealer__c = dealerMap.get( idd.DeliveryInstrument__r.Contract__r.Account__r.RecordType.DeveloperName == 'Dealer' ? idd.DeliveryInstrument__r.Contract__r.Account__c : idd.DeliveryInstrument__r.Contract__r.Maccura__c  );
	    		
	    		if(idd.DeliveryInstrument__r.Contract__c == null &&  idd.ReturnToMainTable__r.InventoryStatus__c  != null  )insa.NEWDealer__c = idd.ReturnToMainTable__r.InvoicingCustomers__r.RecordType.DeveloperName == 'Dealer' ? dealerMap.get(idd.ReturnToMainTable__r.InvoicingCustomers__c  ) :  supMap.get( idd.ReturnToMainTable__r.InventoryStatus__c);
    		//System.debug('genInstallApply==>'+idd.ReturnToMainTable__r.InventoryStatus__c);
	    		//发货明细
	    		insa.InstallInstruments__c = idd.Id;
	    		//安装申请的所有人
	    		insa.OwnerId = idd.ReturnToMainTable__r.OwnerId;
	    		//来自发货申请
	    		insa.FromDeliveryRequisition__c = true;
	    		//安装方式
	    		//insa.InstallationMethod__c = ;

	    		insa.FuselageNumber__c =  enos.isEmpty() ? null : enos.size() > i  ? enos.get(i) :null ;// idd.FuselageNumber__c;

	    		//安装方式
	    		//insa.InstallationType__c = '新装机';
	    		//收货联系人
	    		insa.ContactTelephone__c = idd.ReturnToMainTable__r.ReceiverTelephone__c;
	    		//收货地址
	    		insa.ShippingAddress__c = idd.ReturnToMainTable__r.ReceivingAddress__c;
	    		//收货时间范围
	    		insa.TimeReceipt__c = idd.ReturnToMainTable__r.TimeReceipt__c;
	    		//医院有无特殊要求
	    		insa.RequestReceive__c = idd.ReturnToMainTable__r.RequestReceive__c;
	    		//有无试剂同发
	    		insa.Reagents__c = idd.ReturnToMainTable__r.Reagents__c;
	    		//收货科室
	    		insa.ReceivingDepartment__c = idd.ReturnToMainTable__r.ReceivingDepartment__c;
	    		//仪器要求运达日期
	    		insa.RequestArrivalDate__c = idd.ReturnToMainTable__r.RequestArrivalDate__c;
	    		//仪器进入安装地点有无小台阶
	    		insa.SmallSteps__c =idd.ReturnToMainTable__r.SmallSteps__c;
	    		//最大转角宽度
	    		insa.MaximumCornerWidth__c=idd.ReturnToMainTable__r.MaximumCornerWidth__c;
	    		//台阶几步
	    		insa.StepNo__c=idd.ReturnToMainTable__r.StepNo__c;
	    		//备注
	    		insa.Remarks__c = idd.ReturnToMainTable__r.Remark__c;
	    		//实验室楼层
	    		insa.LaboratoryFloor__c = idd.ReturnToMainTable__r.LaboratoryFloor__c;
	    		//是否有电梯
	    		insa.Elevator__c = idd.ReturnToMainTable__r.Elevator__c;
	    		//楼梯宽度
	    		insa.StaircaseWidth__c = idd.ReturnToMainTable__r.StaircaseWidth__c;
	    		//现场协助人
	    		insa.SiteAssistConsignee__c = idd.ReturnToMainTable__r.SiteAssistConsignee__c;
	    		//电梯尺寸
	    		insa.ElevatorSize__c = idd.ReturnToMainTable__r.ElevatorSize__c;
	    		//电梯载重
	    		insa.ElevatorLoad__c = idd.ReturnToMainTable__r.ElevatorLoad__c;
	    		//科室门口尺寸
	    		insa.DoorwaySize__c = idd.ReturnToMainTable__r.DoorwaySize__c;
	    		//货车进城时间限制 
	    		insa.IntoCityTime__c = idd.ReturnToMainTable__r.IntoCityTime__c;
	    		//是否靠近卸货点
	    		insa.UnloadingPoint__c = idd.ReturnToMainTable__r.UnloadingPoint__c;
	    		//运输通道是否畅通
	    		insa.TransportChannel__c = idd.ReturnToMainTable__r.TransportChannel__c;
	    		//销售员是否在现场
	    		insa.SceneTakeDelivery__c = idd.ReturnToMainTable__r.SceneTakeDelivery__c;
	    		//限制时间段
	    		insa.LimitPeriod__c = idd.ReturnToMainTable__r.LimitPeriod__c;
	    		//合作类型
	    		insa.EnterWay__c = idd.DeliveryInstrument__r.EnterWay__c;

	    		installList.add(insa);

    		}
    		
    	}

    	if(!installList.isEmpty())insert installList;
    }

}